<template>
    <!--<div>
    </div>-->
  <el-container class="home-container">
    <!--头部区-->
    <el-header>
      <div>
        <img src="../assets/yss.png"/>
        <span>公社管理系统</span>
      </div>
      <el-button type="info" @click="logout" >退出</el-button>
    </el-header>
    <el-container>
      <!--左侧边区-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--推拉菜单开关-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域-->
        <el-menu background-color="#708090"
                 text-color="#fff"
                 active-text-color="#409EFF"
                 unique-opened :collapse="isCollapse"
                 :collapse-transition="false" :router="true"
                  :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id +''" v-for="item in menuList" v-bind:key="item.id">
            <!-- 一级菜单模版区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsOjs[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.name}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.href + ''" v-for="subItem in item.child" v-bind:key="subItem.id" @click="saveNavState(subItem.href)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧边区-->
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单
      menuList: [],
      iconsOjs: {
        1: 'el-icon-user',
        7: 'el-icon-setting',
        12: 'el-icon-s-check',
        17: 'el-icon-s-shop',
        21: 'el-icon-s-order',
        23: 'el-icon-data-line'
      },
      // 不折叠
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      // 清空token
      const _this = this
      window.sessionStorage.clear()
      // 跳转到登录页
      _this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const result = await this.$http.get('/permission/getAllMenu')
      if (result.data.code !== 200) return this.$message.error(result.data.msg)
      this.menuList = result.data.data
    },
    // 切换菜单的展开与叠加
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #708090;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div{
    display: flex;
    align-items: center;
    span{
      margin-left: 10px;
    }
  }
  img{
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: #eee;
  }
}
.el-aside{
  background-color: #708090;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: white;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: burlywood;
  color: white;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
